home *** CD-ROM | disk | FTP | other *** search
- Path: news.mindlink.net!news
- From: genew@mindlink.bc.ca (Gene Wirchenko)
- Newsgroups: comp.lang.c
- Subject: Re: division problem
- Date: Sat, 27 Jan 1996 08:32:31 GMT
- Organization: MIND LINK! - British Columbia, Canada
- Message-ID: <4eco1i$aih@fountain.mindlink.net>
- References: <31097D77.11AA@rain.org> <26JAN199622082450@erich.triumf.ca>
- NNTP-Posting-Host: line105.nwm.mindlink.net
- X-Newsreader: Forte Free Agent 1.0.82
-
- bennett@erich.triumf.ca (P.Bennett) wrote:
-
- >In article <31097D77.11AA@rain.org>, tpaul <tpaul@rain.org> writes...
- >>Can anyone show me why this does not work? I am a beginner.
- >>
- >>#include <stdio.h>
- >>
- >>main ()
- >>{
- >> int fahrenheit, celsius;
- >>
- >> printf("Fahrenheit temperature =?";
- >> scanf("%d",fahrenheit);
- ^
- scanf() operates on pointers/addresses. Your statement should
- be:
- scanf("%d", &fahrenheit);
- ^
-
- >> celsius = 5/9*(fahrenheit-32);
-
- >This calculation will be done with ints, and 5/9 in integer arithmetic is 0.
-
- >Also (if I have the precedence rules right), it will be done as:
- > 5
- > ----------------------
- > 9 * (fahrenheit - 32)
-
- Nope! Multiplication and division have the same precedence level
- and are left to right evaluation.
-
- >In order to make it work, you could do:
- > celsius = (int)((5.0/9.0) * (fahrenheit - 32))
- >The 5.0/9.0 give a meaningful result, and force the multiplication to be done
- >with floats. The (int) will convert the result back to an int.
-
- A further note: be sure that you cast the result of the entire
- calculation to int, not just the 5/9 part or you get the same problem.
-
- The printf() from the original post is mising and wrong. It's
- missing the closing right paren.
-
- >Peter Bennett VE7CEI | Vessels shall be deemed to be in sight
- >Internet: bennett@triumf.ca | of one another only when one can be
- >Packet: ve7cei@ve7kit.#vanc.bc.ca | observed visually from the other
- >TRIUMF, Vancouver, B.C., Canada | ColRegs 3(k)
- >GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
-
- Sincerely,
-
- Gene Wirchenko
-
- C Pronunciation Guide:
- y=x++; "wye equals ex plus plus semicolon"
- x=x++; "ex equals ex doublecross semicolon"
-
-